﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling.Shell;
using System.Diagnostics;

namespace Demo.AgilePlanner
{
    internal partial class AgilePlannerCommandSet
    {
        public Guid cmdExportGUID = new Guid("62FBE4B9-6899-4b2a-867E-E686842FEE91");
        public const int cmdExportID = 0x801;

        public Guid cmdColorStatusGUID = new Guid("6AD7B9EF-F0A2-42b3-8437-E956DFD0CCBA");
        public const int cmdColorStatusID = 0x802;

        public Guid cmdColorPriorityGUID = new Guid("9D741D19-C0DC-4148-BDB0-5E540839511A");
        public const int cmdColorPriorityID = 0x803;

        protected override IList<MenuCommand> GetMenuCommands()
        {

            IList<MenuCommand> commands = base.GetMenuCommands();

            commands.Add(new DynamicStatusMenuCommand(
                new EventHandler(OnPopupMenuStatus),
                new EventHandler(OnPopupMenuClick),
                new CommandID(cmdExportGUID, cmdExportID)));

            commands.Add(new DynamicStatusMenuCommand(
                new EventHandler(OnPopupMenuStatus),
                new EventHandler(OnPopupMenuClick),
                new CommandID(cmdColorStatusGUID, cmdColorStatusID)));

            commands.Add(new DynamicStatusMenuCommand(
                new EventHandler(OnPopupMenuStatus),
                new EventHandler(OnPopupMenuClick),
                new CommandID(cmdColorPriorityGUID, cmdColorPriorityID)));

            return commands;

        }

        internal void OnPopupMenuStatus(object sender, EventArgs e)
        {
            MenuCommand cmd = sender as MenuCommand;
            switch (cmd.CommandID.ID)
            {
                case cmdColorStatusID:
                    cmd.Checked = IsColorSchemaOnStatus();
                    break;

                case cmdColorPriorityID:
                    cmd.Checked = !IsColorSchemaOnStatus();
                    break;
            }
        }

        internal void OnPopupMenuClick(object sender, EventArgs e)
        {
            MenuCommand cmd = sender as MenuCommand;

            switch (cmd.CommandID.ID)
            {
                case cmdExportID:
                    ExportImage();
                    break;

                case cmdColorStatusID:
                    ChangeColorSchema(ColorSchema.Status);
                    break;

                case cmdColorPriorityID:
                    ChangeColorSchema(ColorSchema.Priority);
                    break;
            }

        }

        private void ExportImage()
        {
            SaveFileDialog sd = new SaveFileDialog();
            sd.DefaultExt = "*.png";
            sd.FileName = CurrentDocView.CurrentDiagram.Name + ".png";
            sd.OverwritePrompt = true;
           
            if (sd.ShowDialog() == DialogResult.OK)
            {
                Diagram diagram = CurrentDocView.CurrentDiagram;
                using (Bitmap bmp = diagram.CreateBitmap(
                    diagram.NestedChildShapes,
                    Diagram.CreateBitmapPreference.FavorClarityOverSmallSize))
                {
                    bmp.Save(sd.FileName, ImageFormat.Png);
                }

            }
        }

        private Project GetProject()
        {
            Project project = CurrentDocView.CurrentDiagram.ModelElement as Project;
            Debug.Assert(project != null, "project cannot be null");
            return project;
        }

        private bool IsColorSchemaOnStatus()
        {
            return GetProject().ColorSchema == ColorSchema.Status;
        }

        private void ChangeColorSchema(ColorSchema colorSchema)
        {
            using(CurrentDocData.Store.TransactionManager.BeginTransaction("change color schema"))
            {
                GetProject().ColorSchema = colorSchema;
                foreach (ShapeElement shape in CurrentDocView.CurrentDiagram.NestedChildShapes)
                {
                    ChangeColorSchema(shape, colorSchema);
                }
                CurrentDocData.Store.TransactionManager.CurrentTransaction.Commit();
            }
            
            CurrentDocView.CurrentDiagram.Invalidate();
        }

        private void ChangeColorSchema(ShapeElement shapeElement, ColorSchema colorSchema)
        {
            if (shapeElement == null) return;

            if (shapeElement is StoryShape)
            {
                ((StoryShape)shapeElement).UpdateFillColor();
            }
            else
            {
                foreach (ShapeElement shape in shapeElement.NestedChildShapes)
                {
                    ChangeColorSchema(shape, colorSchema);
                }
            }
        }
    }
}
